package com.technician.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.technician.constant.CommonConstant;
import com.technician.exceptionHandler.GlobalSystemException;
import com.technician.mapper.GoodsMapper;
import com.technician.model.dto.goods.GoodsQueryRequest;
import com.technician.model.entity.Goods;
import com.technician.service.GoodsService;
import com.technician.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Override
    public QueryWrapper<Goods> getQueryWrapper(GoodsQueryRequest goodsQueryRequest) {
        if (goodsQueryRequest == null) {
            throw new GlobalSystemException(400,"请求参数异常");
        }

        //todo 也可以根据价格范围，标签
        BigDecimal price = goodsQueryRequest.getPrice();
        String tags = goodsQueryRequest.getTags();

        Long id = goodsQueryRequest.getId();
        String name = goodsQueryRequest.getName();
        Long type = goodsQueryRequest.getType();
        String sortField = goodsQueryRequest.getSortField();
        String sortOrder = goodsQueryRequest.getSortOrder();

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.eq(Objects.nonNull(type),"type",type);
        queryWrapper.eq(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}
